package net.tsz.afinal;

import java.lang.reflect.Field;

import net.tsz.afinal.annotation.view.EventListener;
import net.tsz.afinal.annotation.view.Select;
import net.tsz.afinal.annotation.view.ViewInject;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.AbsListView;

public class AnnotationUtils {

	public static void initView(Context context, View view, Object viewHolder) {
		Field[] fields = viewHolder.getClass().getDeclaredFields();
		if (fields != null && fields.length > 0) {
			for (Field field : fields) {
				ViewInject viewInject = field.getAnnotation(ViewInject.class);
				if (viewInject != null) {
					int viewId = viewInject.id();
					try {
						field.setAccessible(true);
						field.set(viewHolder, view.findViewById(viewId));
					} catch (Exception e) {
						e.printStackTrace();
					}

					String clickMethod = viewInject.click();
					if (!TextUtils.isEmpty(clickMethod))
						setViewClickListener(field, clickMethod, viewHolder);

					String longClickMethod = viewInject.longClick();
					if (!TextUtils.isEmpty(longClickMethod))
						setViewLongClickListener(field, longClickMethod,
								viewHolder);

					String itemClickMethod = viewInject.itemClick();
					if (!TextUtils.isEmpty(itemClickMethod))
						setItemClickListener(field, itemClickMethod, viewHolder);

					String itemLongClickMethod = viewInject.itemLongClick();
					if (!TextUtils.isEmpty(itemLongClickMethod))
						setItemLongClickListener(field, itemLongClickMethod,
								viewHolder);

					Select select = viewInject.select();
					if (!TextUtils.isEmpty(select.selected()))
						setViewSelectListener(field, select.selected(),
								select.noSelected(), viewHolder);

				}
			}
		}
	}

	private static void setViewClickListener(Field field, String clickMethod,
			Object viewHolder) {
		try {
			Object obj = field.get(viewHolder);
			if (obj instanceof View) {
				((View) obj).setOnClickListener(new EventListener(viewHolder)
						.click(clickMethod));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static void setViewLongClickListener(Field field,
			String clickMethod, Object viewHolder) {
		try {
			Object obj = field.get(viewHolder);
			if (obj instanceof View) {
				((View) obj).setOnLongClickListener(new EventListener(
						viewHolder).longClick(clickMethod));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static void setItemClickListener(Field field,
			String itemClickMethod, Object viewHolder) {
		try {
			Object obj = field.get(viewHolder);
			if (obj instanceof AbsListView) {
				((AbsListView) obj).setOnItemClickListener(new EventListener(
						viewHolder).itemClick(itemClickMethod));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static void setItemLongClickListener(Field field,
			String itemClickMethod, Object viewHolder) {
		try {
			Object obj = field.get(viewHolder);
			if (obj instanceof AbsListView) {
				((AbsListView) obj)
						.setOnItemLongClickListener(new EventListener(
								viewHolder).itemLongClick(itemClickMethod));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static void setViewSelectListener(Field field, String select,
			String noSelect, Object viewHolder) {
		try {
			Object obj = field.get(viewHolder);
			if (obj instanceof View) {
				((AbsListView) obj)
						.setOnItemSelectedListener(new EventListener(viewHolder)
								.select(select).noSelect(noSelect));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
